package com.example.datamanage.controller;

import com.example.datamanage.common.Result;
import com.example.datamanage.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 28142
 * @description 数据查询 controller
 * @date 2023/7/26 17:26
 */
@RestController
@RequestMapping("/data")
public class DataController {
    @Autowired
    private DataService dataService;

    /**
     * 查询传入的数据库下所有表
     * @author 杨雨盛
     * @date 2023/7/31 10:06
     * @param database
     * @return com.example.datamanage.common.Result
     */
    @GetMapping("/tables/{type}/{database}")
    public Result showTables(@PathVariable String type,
                             @PathVariable String database) {
        return dataService.showTables(type, database);
    }

    /**
     * 查询传入的数据库下所有视图
     * @author 杨雨盛
     * @date 2023/7/31 10:07
     * @param database
     * @return com.example.datamanage.common.Result
     */
    @GetMapping("/views/{type}/{database}")
    public Result showViews(@PathVariable String type,
                            @PathVariable String database) {
        return dataService.showViews(type, database);
    }

    /**
     * 查询传入数据库下所有表和视图
     * @author 杨雨盛
     * @date 2023/7/31 15:57
     * @param database
     * @return com.example.datamanage.common.Result
     */
    @GetMapping("/tables/all/{database}")
    public Result showAllTables(@PathVariable String database) {
        return dataService.showAllTables(database);
    }


    @GetMapping("/detail/{database}/{table}")
    public Result showTableDetails(@PathVariable String database,
                                   @PathVariable String table) {
        return dataService.showColumns(database, table);
    }

    /**
     * 根据条件分页查询库下的表
     * @author 杨雨盛
     * @date 2023/8/1 14:34
     * @param database
     * @param chName
     * @param enName
     * @return com.example.datamanage.common.Result
     */
    @GetMapping("/tables/{database}/{pageNum}/{pageSize}")
    public Result showTablesByCondition(@PathVariable String database,
                           @PathVariable Integer pageNum,
                           @PathVariable Integer pageSize,
                           @RequestParam(required = false) String chName,
                           @RequestParam(required = false) String enName) {
        return dataService.showTablesByCondition(database, pageNum, pageSize, chName, enName);
    }

    @GetMapping("/export/{table}/{fileName}/{type}")
    public void fullExport(@PathVariable String table,
                             @PathVariable String fileName,
                             @PathVariable String type,
                             @RequestParam List<String> fields,
                             HttpServletResponse response) {
        dataService.fullExport(table, fileName, type, fields, response);
    }

    /**
     * 展示数据库基本信息
     * @author 杨雨盛
     * @date 2023/8/2 16:54
     * @param database
     * @param pageNum
     * @param pageSize
     * @return com.example.datamanage.common.Result
     */
    @GetMapping("/info/{database}/{type}/{pageNum}/{pageSize}")
    public Result getInfo(@PathVariable String database,
                          @PathVariable String type,
                          @PathVariable Integer pageNum,
                          @PathVariable Integer pageSize) {
        return dataService.getInfo(database, type, pageNum, pageSize);
    }

}
